package com.bulletelc.apollopre.module.main.center.quotation;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.QuotationBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/5/16.
 */

public class QuotationListPresenter extends BasePresenter<QuotationListContract.View> implements QuotationListContract.Persenter {
    @Override
    public void getQuotationList(int intention_form_id) {
        getQuotationListImp(intention_form_id);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    private void getQuotationListImp(int intention_form_id) {
        String json = "";
        JSONObject object = new JSONObject();
        try {
            object.put("intention_form_id", intention_form_id);
            object.put("state", "[10, 20, 30, 40]");
            json = object.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final RequestBody requestBody = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_QUOTATION_ORDER_LIST)
                .headers(NetWorkPath.HEADER_KEY_AUTH, NetWorkPath.AUTH_TOKEN_PRE + MyApplication.getToken())
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseQuotationList(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().onErrorQuotationList(response.getException().toString());
                    }
                });
    }

    private void parseQuotationList(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<QuotationBean>>() {
                    }.getType();
                    List<QuotationBean> quotationBeanList = gson.fromJson(object.getJSONArray("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessQuotationList(quotationBeanList);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailQuotationList(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailQuotationList(response.body().toString());
            }
        }
    }
}
